<?php

namespace app\models;

class Group extends BaseModel
{
    //表单传入字段
    public $groupName  = '';
    public $groupAcl   = '';
    public $statusIs   = '';
    public $createTime = '';
    public $updateTime = '';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%admin_group}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['groupName'], 'required', 'message' => '必须填写'],
            ['groupName', 'unique', 'message' => '该分组名称已经存在'],
            [['groupName'], 'string', 'max' => 50, 'message' => '分组名称不能超过50个字符长度'],
            ['statusIs', 'in', 'range' => [0, 1], 'message' => '无效状态值'],
            [['groupAcl', 'createTime', 'updateTime'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'GroupId',
            'groupName'  => 'GroupName',
            'groupAcl'   => 'GroupAcl',
            'createTime' => 'GroupCreateTime',
            'statusIs'   => 'GroupStatus',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert) {
                $this->createTime    = time();
                $this->updateTime    = time();
            } else {
                $this->updateTime = time();
            }
            return true;
        } else {
            return false;
        }
    }
}
